package com.gamex.findit.controller.web;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.gamex.findit.Constants;
import com.gamex.findit.internal.DefaultConfig;
import com.gamex.findit.model.ADImage;
import com.gamex.findit.model.Image;
import com.gamex.findit.service.ADImageService;
import com.gamex.findit.util.CommonUtil;
import com.gamex.findit.util.Generator;

@Controller
public class ADImageController {
	
	private static final Logger logger = LoggerFactory.getLogger(ADImageController.class);
	
	@Autowired ADImageService adImageService;
	@Autowired DefaultConfig config;
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
	    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	  
	    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param adImage
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/adImageList")
	public String adImageList( HttpSession session
			  			   , HttpServletRequest request
						   , HttpServletResponse response
						   , ModelMap model
						   , @ModelAttribute ADImage adImage) throws Exception{
	
	List<ADImage>	result = adImageService.getAD(adImage);
	int count = adImageService.getADCount(adImage);
	
	model.addAttribute("list",result);
	model.addAttribute("count", count);
	model.addAttribute("pageNo", adImage.getPageNo());
	model.addAttribute("pageGroup", adImage.getPageGroup());
	model.addAttribute("searchParam", adImage);
	return "/adImage/adImageList";
	}

	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param adImage
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/viewAdImage")
	public String AdImageView( HttpSession session
						   , HttpServletRequest request
						   , HttpServletResponse response
						   , Model model
						   , @ModelAttribute ADImage adImage) throws Exception{
		
	model.addAttribute("view", adImageService.getADView(adImage));
	model.addAttribute("searchParam", adImage);
	return "/adImage/viewAdImage";
	}	
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param adImage
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/deleteAdImage")
	public String deleteAdImage( HttpSession session
							  , HttpServletRequest request
							  , HttpServletResponse response
							  , Model model
							  , @ModelAttribute ADImage adImage) throws Exception{
		
		adImageService.deleteAD(adImage.getAdId());
		
	return "redirect:/admin/adImageList.json";
	}
	
	/**
	 * @param request
	 * @param response
	 * @param adImage
	 * @param file
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/addAdImage")
	public String addAdImage( HttpServletRequest request
							 , HttpServletResponse response
							 , @ModelAttribute ADImage adImage
							 , @RequestParam("file") MultipartFile  file
							 , Model model) throws Exception{

		String name = Generator.generateFileName();
		if(file.getSize() > 0){
			CommonUtil.writeImage(request, file, Constants.DEFAULT_AD_IMG_PATH, name);
			adImage.setImg(String.format(Constants.DEFAULT_AD_URL, config.getHostAddress()) + name);
		}
		
		adImage.setImgId(adImage.getImgId());
		adImageService.insertAD(adImage);
		
	return "redirect:/admin/adImageList.json";
	}

	/**
	 * @param request
	 * @param response
	 * @param adImage
	 * @param file
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/updateAdImage")
	public String updateAdImage( HttpServletRequest request
							 , HttpServletResponse response
							 , @ModelAttribute ADImage adImage
							 , @RequestParam("file") MultipartFile  file
							 , ModelMap model) throws Exception{

		String name = Generator.generateFileName();
		if(file.getSize() > 0){
			CommonUtil.writeImage(request, file, Constants.DEFAULT_AD_IMG_PATH, name);
			adImage.setImg(String.format(Constants.DEFAULT_AD_URL, config.getHostAddress()) + name);
		}
		
		adImage.setImgId(adImage.getImgId());
		adImageService.updateAD(adImage);
		
	return "redirect:/admin/adImageList.json";
	}
	

}
